import Request from "@/api/request";
import store from "@/store";
// in .ts file use this router; in .vue use useRouter()
import { IncomeStream } from "@/interface/entity";
import {
  generateIncomeStreamLists,
  processIncomeStreamBeforeToDb
} from "@/functions/incomestream";

export async function addIncomeStreamToDb(incomeStream: IncomeStream) {
  const incomeStreamToDb = processIncomeStreamBeforeToDb(incomeStream, "add");
  Request({
    method: "post",
    url: "IncomeStream/AddIncomeStream",
    data: JSON.stringify(incomeStreamToDb)
  });
}
export async function deleteIncomeStreamInDb(incomeStream: IncomeStream) {
  await Request({
    method: "delete",
    url: "IncomeStream/DeleteIncomeStream",
    params: {
      streamID: incomeStream.streamID
    }
  });
}
// update, and the oleincomeamount is stored in the incomestream object
export async function upDateIncomeStreamToDb(incomeStream: IncomeStream) {
  const incomeStreamToDb = processIncomeStreamBeforeToDb(
    incomeStream,
    "update"
  );
  Request({
    method: "put",
    url: "IncomeStream/UpDateIncomeStream",
    data: JSON.stringify(incomeStreamToDb)
  });
}

export async function queryAllIncomeStream() {
  await Request({
    method: "get",
    url: "IncomeStream/QueryAllIncomeStream"
  })
    .then(response => {
      // update store
      store.state.tableData = generateIncomeStreamLists(
        response.data as IncomeStream[]
      );
      console.log(store.state.tableData.length, "incomestream");
    })
    .catch(err => {
      console.log(err);
      store.state.tableData = [];
    });
}
